//
//  WBWelcomeView.swift
//  CustomWeiBo
//
//  Created by laiqiang on 21/5/20.
//  Copyright © 2021年 laiqiang. All rights reserved.
//

import UIKit
import SDWebImage

//欢迎视图
class WBWelcomeView: UIView {

    @IBOutlet weak var iconImgView: UIImageView!
    
    @IBOutlet weak var lable: UILabel!

    @IBOutlet weak var imgBottom: NSLayoutConstraint!

    class func welcomeView()->WBWelcomeView {

        let nib = UINib(nibName: "WBWelcomeView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBWelcomeView
        v.frame = UIScreen.main.bounds

        return v
    }
    
    override func awakeFromNib() {
        
        guard let urlString = WBNetworkingManager.share.userAccount.avatar_large,
        let url = URL.init(string: urlString)
        else {
            return
        }
        
        iconImgView.sd_setImage(with: url,
                                placeholderImage: UIImage.init(named:"mine_active"))
    }
    
    //视图 添加到window上， 已经显示
    override func didMoveToWindow() {
        
        self.layoutIfNeeded()//按照当前约束更新 (nib设置的)
        
        imgBottom.constant = 100//修改nib设置的
        
        UIView.animate(withDuration: 3.0,
                       delay: 0,
                       usingSpringWithDamping: 0.7,//弹跳阻尼 (弹跳范围高度)
                       initialSpringVelocity: 0,//初速度
                       options: [],
                       animations: {
                        
                        //立刻更新 约束 （修改过的）
                        self.layoutIfNeeded()
        }) { (isSucces) in
            print("动画完成")
            
            UIView.animate(withDuration: 1.0,
                           animations: { 
                            self.lable.alpha = 1.0
            }, completion: { (_) in
                self.removeFromSuperview()
            })
        }
    }
}
